import {ecs} from "db://oops-framework/libs/ecs/ECS";
import {pb} from "db://assets/script/network/ws/proto";
import {Long} from "protobufjs";
import {getNewTakeTileFromDuanPai} from "db://assets/script/utils/protocolUtils";

// 加入麻将房，未在房内
@ecs.register("InitMjDeskComp")
export class InitMjDeskComp extends ecs.Comp {
    reset(): void {
        this.deskInfo = null
        this.playerList = null
        this.categoryConf = null
        this.reconnect = false
        this.onFinish = null
        this.roundInfo = null
        this.newTile = null
    }
    // 房间信息
    deskInfo: pb.IDeskInfo
    // 玩家列表
    playerList: pb.IPlayerInfo[]
    // 金豆房规格
    categoryConf: pb.ICategoryItem
    reconnect: boolean
    playerCards: pb.IDuanPaiInfo[]
    onFinish?: Function
    roundInfo: pb.IDeskRoundInfo
    newTile: pb.ITileInfo
    playerMap() {
        const players = {}
        for (const p of this.playerList) {
            players[Number(p.playerInfo.shortId)] = p
        }
        return players
    }

    // 获取新摸的牌
    getNewTile(shortId: number | Long) {
        // 是否轮到本人出牌
        const playerInfos = this.playerList.filter(value => {
            return value.playerInfo.shortId === shortId
        })
        if (playerInfos.length !== 1) {
            return
        }
        const isMyTurn =  this.roundInfo && playerInfos[0].playerDeskInfo.pos === this.roundInfo.curTurn
        if (isMyTurn) {
            this.newTile = getNewTakeTileFromDuanPai(this.playerCards, shortId)
        }
    }

}
